
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

extern "C" {
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ysip.h"
#include "../strtopinyin.h"
}
#include<exception>
#include<iostream>
#include "../ysip.hpp"
using namespace std;
/* If you declare any globals in php_ysip.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(ysip)
*/

/* True global resources - no need for thread safety here */
static int le_ysip;



zval * region_childs_zvalize(REGION_INFO_X ** region_childs, int len) {
	int i = 0; 
	zval * child, *childs;
	MAKE_STD_ZVAL(childs);
	array_init(childs);
	for( i = 0 ; i < len; i++) {
		MAKE_STD_ZVAL(child);
		array_init(child);
		add_assoc_long(child, (char *) "id", region_childs[i]->info.id);
		add_assoc_long(child, (char *) "parent_id", region_childs[i]->info.parent_id);
		add_assoc_long(child, (char *) "level", region_childs[i]->info.level);
		add_assoc_long(child, (char *) "length", region_childs[i]->length);
		add_assoc_long(child, (char *) "center_id", region_childs[i]->info.center_id);
		add_assoc_string(child, (char *) "name", region_childs[i]->info.name, 1);
		add_assoc_string(child, (char *) "en_name", region_childs[i]->info.en_name, 1);
		if(region_childs[i]->childs != NULL && region_childs[i]->length > 0) {
			add_assoc_zval(child, (char *) "childs", region_childs_zvalize( region_childs[i]->childs, region_childs[i]->length));
		} else {
			add_assoc_bool(child, (char *) "childs", 0);
		}
		add_next_index_zval(childs, child);
	}
	return childs;
}
/* {{{ ysip_functions[]
 *
 * Every user visible function must have an entry in ysip_functions[].
 */
zend_function_entry ysip_functions[] = {
	PHP_FE(confirm_ysip_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(ysip_get_ip_info,	NULL)
	// compat for old version
	PHP_FE(ysip_get_ipinfo,	NULL)
	PHP_FE(ysip_get_region_info_by_name,	NULL)
	PHP_FE(ysip_get_region_info_by_name_ex,	NULL)
	PHP_FE(ysip_get_region_info_by_en_name,	NULL)
	PHP_FE(ysip_get_region_info,	NULL)
	PHP_FE(ysip_get_region_parents,	NULL)
	PHP_FE(ysip_get_region_childs,	NULL)
	PHP_FE(ysip_get_all_regions,	NULL)
	PHP_FE(strtopinyin, NULL)
	{NULL, NULL, NULL}	/* Must be the last line in ysip_functions[] */
};
/* }}} */

/* {{{ ysip_module_entry
 */
zend_module_entry ysip_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	(char *)"ysip",
	ysip_functions,
	PHP_MINIT(ysip),
	PHP_MSHUTDOWN(ysip),
	PHP_RINIT(ysip),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(ysip),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(ysip),
#if ZEND_MODULE_API_NO >= 20010901
	(char *)"0.1", /* Replace with version number for your extension */
#endif
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_YSIP
ZEND_GET_MODULE(ysip)
#endif

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("ysip.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_ysip_globals, ysip_globals)
    STD_PHP_INI_ENTRY("ysip.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_ysip_globals, ysip_globals)
PHP_INI_END()
*/
/* }}} */

/* {{{ php_ysip_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_ysip_init_globals(zend_ysip_globals *ysip_globals)
{
	ysip_globals->global_value = 0;
	ysip_globals->global_string = NULL;
}
*/
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(ysip)
{
	/* If you have INI entries, uncomment these lines 
	REGISTER_INI_ENTRIES();
	*/
	if( ! get_instance() ) {
		php_error(E_ERROR, "Cann't get instance !");
	}
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(ysip)
{
	/* uncomment this line if you have INI entries
	UNREGISTER_INI_ENTRIES();
	*/
	release_instance();
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(ysip)
{
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(ysip)
{
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(ysip)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "ysip support", "enabled");
	php_info_print_table_end();

	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}
/* }}} */


/* Remove the following function when you have succesfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_ysip_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_ysip_compiled)
{
	char *arg = NULL;
	int arg_len, len;
	char *strg;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char *)"s", &arg, &arg_len) == FAILURE) {
		return;
	}

	len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "ysip", arg);
	RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and 
   unfold functions in source code. See the corresponding marks just before 
   function definition, where the functions purpose is also documented. Please 
   follow this convention for the convenience of others editing your code.
*/

/* {{{ proto array ysip_get_ip_info(string ip)
   ; */
PHP_FUNCTION(ysip_get_ip_info)
{
	char *ip = NULL;
	int argc = ZEND_NUM_ARGS();
	int ip_len;
	REGION_INFO region_info;

	if (zend_parse_parameters(argc TSRMLS_CC, (char *)"s", &ip, &ip_len) == FAILURE) {
		php_error(E_WARNING, "Parameter is not valid, only IP String is ok!");
	}
	if(get_ip_info(ip, & region_info)) {
		array_init(return_value);
		add_assoc_long(return_value, (char *) "id", region_info.id);
		add_assoc_long(return_value, (char *) "parent_id", region_info.parent_id);
		add_assoc_long(return_value, (char *) "level", region_info.level);
		add_assoc_long(return_value, (char *) "center_id", region_info.center_id);
		add_assoc_string(return_value, (char *) "name", region_info.name, 1);
		add_assoc_string(return_value, (char *) "en_name", region_info.en_name, 1);
	} else {
		RETURN_FALSE;
	}
}

// compat for old version
/* {{{ proto array ysip_get_ipinfo(string ip)
   ; */
PHP_FUNCTION(ysip_get_ipinfo)
{
	char *ip = NULL;
	int argc = ZEND_NUM_ARGS();
	int ip_len;
	REGION_INFO region_info;

	if (zend_parse_parameters(argc TSRMLS_CC, (char *)"s", &ip, &ip_len) == FAILURE) {
		php_error(E_WARNING, "Parameter is not valid, only IP String is ok!");
	}
	if(get_ip_info(ip, & region_info)) {
		array_init(return_value);
		add_assoc_long(return_value, (char *) "code", region_info.id);
		add_assoc_string(return_value, (char *) "city", region_info.name, 1);
		add_assoc_string(return_value, (char *) "prov", region_info.name, 1);
		/*
		add_assoc_long(return_value, (char *) "parent_id", region_info.parent_id);
		add_assoc_long(return_value, (char *) "level", region_info.level);
		add_assoc_long(return_value, (char *) "center_id", region_info.center_id);
		add_assoc_string(return_value, (char *) "en_name", region_info.en_name, 1);
		*/
	} else {
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto array ysip_get_region_info_by_name(string name)
   ; */
PHP_FUNCTION(ysip_get_region_info_by_name)
{
	char *name = NULL;
	int argc = ZEND_NUM_ARGS();
	int name_len;

	if (zend_parse_parameters(argc TSRMLS_CC, (char *)"s", &name, &name_len) == FAILURE) {
		php_error(E_WARNING, "Parameter is invalid!");
	}
	REGION_INFO region_info;
	if(get_region_info_by_name(name, &region_info)) {
		array_init(return_value);
		add_assoc_long(return_value, (char *) "id", region_info.id);
		add_assoc_long(return_value, (char *) "parent_id", region_info.parent_id);
		add_assoc_long(return_value, (char *) "level", region_info.level);
		add_assoc_long(return_value, (char *) "center_id", region_info.center_id);
		add_assoc_string(return_value, (char *) "name", region_info.name, 1);
		add_assoc_string(return_value, (char *) "en_name", region_info.en_name, 1);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */


/* {{{ proto array ysip_get_region_info_by_en_name(string name)
   ; */
PHP_FUNCTION(ysip_get_region_info_by_en_name)
{
	char *name = NULL;
	int argc = ZEND_NUM_ARGS();
	int name_len;

	if (zend_parse_parameters(argc TSRMLS_CC, (char *)"s", &name, &name_len) == FAILURE) {
		php_error(E_WARNING, "Parameter is invalid!");
	}
	REGION_INFO region_info;
	if(get_region_info_by_en_name(name, &region_info)) {
		array_init(return_value);
		add_assoc_long(return_value, (char *) "id", region_info.id);
		add_assoc_long(return_value, (char *) "parent_id", region_info.parent_id);
		add_assoc_long(return_value, (char *) "level", region_info.level);
		add_assoc_long(return_value, (char *) "center_id", region_info.center_id);
		add_assoc_string(return_value, (char *) "name", region_info.name, 1);
		add_assoc_string(return_value, (char *) "en_name", region_info.en_name, 1);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */
/* {{{ proto array get_region_info(int region_id)
   ; */
PHP_FUNCTION(ysip_get_region_info)
{
	int argc = ZEND_NUM_ARGS();
	long region_id;

	if (zend_parse_parameters(argc TSRMLS_CC, (char*)"l", &region_id) == FAILURE)  {
		php_error(E_WARNING, "Parameter is invalid !");
	}

	REGION_INFO region_info;
	if(get_region_info(region_id, &region_info)) {
		array_init(return_value);
		add_assoc_long(return_value, (char *) "id", region_info.id);
		add_assoc_long(return_value, (char *) "parent_id", region_info.parent_id);
		add_assoc_long(return_value, (char *) "level", region_info.level);
		add_assoc_long(return_value, (char *) "center_id", region_info.center_id);
		add_assoc_string(return_value, (char *) "name", region_info.name, 1);
		add_assoc_string(return_value, (char *) "en_name", region_info.en_name, 1);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto array ysip_get_region_parents(int region_id)
   ; */
PHP_FUNCTION(ysip_get_region_parents)
{
	int argc = ZEND_NUM_ARGS();
	long region_id;
	int len = 0, i = 0;
	zval * parent;

	if (zend_parse_parameters(argc TSRMLS_CC, (char *)"l", &region_id) == FAILURE)  {
		php_error(E_WARNING, "Parameter is invalid!");	
	}
	REGION_INFO ** region_info = get_region_parents(region_id, len);
	if(region_info != NULL && len > 0) {
		array_init(return_value);
		for( i = 0 ; i < len ; i ++) {
			MAKE_STD_ZVAL(parent);
			array_init(parent);
			add_assoc_long(parent, (char *) "id", region_info[i]->id);
			add_assoc_long(parent, (char *) "parent_id", region_info[i]->parent_id);
			add_assoc_long(parent, (char *) "level", region_info[i]->level);
			add_assoc_long(parent, (char *) "center_id", region_info[i]->center_id);
			add_assoc_string(parent, (char *) "name", region_info[i]->name, 1);
			add_assoc_string(parent, (char *) "en_name", region_info[i]->en_name, 1);
			add_next_index_zval(return_value, parent);
		}
		free_region_info(region_info, len);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto array ysip_get_region_childs(int region_id)
   ; */
PHP_FUNCTION(ysip_get_region_childs)
{
	int argc = ZEND_NUM_ARGS();
	long region_id;
	long depth;
	int len = 0, i = 0;
	zval * child;

	if(argc == 2) {
		if (zend_parse_parameters(argc TSRMLS_CC, (char *)"ll", &region_id, &depth) == FAILURE)  {
			php_error(E_WARNING, "Parameter is invalid!");	
		}
	} else if (argc == 1) {
		if (zend_parse_parameters(argc TSRMLS_CC, (char *)"l", &region_id) == FAILURE)  {
			php_error(E_WARNING, "Parameter is invalid!");	
		}
		depth = 1;
	} else {
		WRONG_PARAM_COUNT;
	}
	REGION_INFO_X * region_info_x = get_region_childs(region_id, depth);
	if(region_info_x != NULL) {
		array_init(return_value);
		add_assoc_long(return_value, (char *) "id", region_info_x->info.id);
		add_assoc_long(return_value, (char *) "parent_id", region_info_x->info.parent_id);
		add_assoc_long(return_value, (char *) "level", region_info_x->info.level);
		add_assoc_long(return_value, (char *) "length", region_info_x->length);
		add_assoc_long(return_value, (char *) "center_id", region_info_x->info.center_id);
		add_assoc_string(return_value, (char *) "name", region_info_x->info.name, 1);
		add_assoc_string(return_value, (char *) "en_name", region_info_x->info.en_name, 1);
		if( region_info_x->length > 0) {
			add_assoc_zval(return_value, (char *) "childs", region_childs_zvalize(region_info_x->childs, region_info_x->length));
		} else {
			add_assoc_bool(return_value, (char *) "childs", 0);
		}
		free_region_info_x(region_info_x);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto array ysip_get_all_regions()
   ; */
PHP_FUNCTION(ysip_get_all_regions)
{
	int len = 0;
	int i = 0;
	zval * region;
	if (ZEND_NUM_ARGS() != 0) {
		WRONG_PARAM_COUNT;
	}
	REGION_INFO ** region_info = get_all_regions(len);

	if( len > 0 ) {
		array_init(return_value);
		for(i = 0 ; i < len; i ++) {
			MAKE_STD_ZVAL(region);
			array_init(region);
			add_assoc_long(region, (char *) "id", region_info[i]->id);
			add_assoc_long(region, (char *) "parent_id", region_info[i]->parent_id);
			add_assoc_long(region, (char *) "level", region_info[i]->level);
			add_assoc_long(region, (char *) "center_id", region_info[i]->center_id);
			add_assoc_string(region, (char *) "name", region_info[i]->name, 1);
			add_assoc_string(region, (char *) "en_name", region_info[i]->en_name, 1);
			add_next_index_zval(return_value, region);
		}
		free_region_info(region_info, len);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */

/* {{{ proto array ysip_get_region_info_by_name_ex()
   ; */
PHP_FUNCTION(ysip_get_region_info_by_name_ex)
{
	char *name = NULL;
	int argc = ZEND_NUM_ARGS();
	int name_len;
	int len = 0;
	int i = 0;
	zval * region;

	if (zend_parse_parameters(argc TSRMLS_CC, (char *)"s", &name, &name_len) == FAILURE) {
		php_error(E_WARNING, "Parameter is invalid!");
	}

	REGION_INFO ** region_info = get_region_info_by_name_ex(name, len);

	if( len > 0 ) {
		array_init(return_value);
		for(i = 0 ; i < len; i ++) {
			MAKE_STD_ZVAL(region);
			array_init(region);
			add_assoc_long(region, (char *) "id", region_info[i]->id);
			add_assoc_long(region, (char *) "parent_id", region_info[i]->parent_id);
			add_assoc_long(region, (char *) "level", region_info[i]->level);
			add_assoc_long(region, (char *) "center_id", region_info[i]->center_id);
			add_assoc_string(region, (char *) "name", region_info[i]->name, 1);
			add_assoc_string(region, (char *) "en_name", region_info[i]->en_name, 1);
			add_next_index_zval(return_value, region);
		}
		free_region_info(region_info, len);
	} else {
		RETURN_FALSE;
	}
}
/* }}} */

/** {{{ proto string strtopinyin(string str, string encode);
 *
 **/
PHP_FUNCTION(strtopinyin)
{
	int argc = ZEND_NUM_ARGS();
	char *str = NULL;
	int str_len;

	char *encode = NULL;
	int encode_len;
	char *ret;
	if(argc == 1) {
		if (zend_parse_parameters(argc TSRMLS_CC, (char *)"s", &str, &str_len) == FAILURE) {
			php_error(E_WARNING, "Parameter is invalid!");
		}
	} else if(argc == 2) {
		if(zend_parse_parameters(argc TSRMLS_CC, (char *)"ss", &str, &str_len, &encode, &encode_len) == FAILURE) {
			php_error(E_WARNING, "Parameter is invalid!");
		}
	} else {
		WRONG_PARAM_COUNT;
		return;
	}
	ret = strtopinyin(str, encode == NULL ? "UTF-8" : encode);
	RETVAL_STRINGL(ret, strlen(ret) + 1, 1);
	free(ret);
}
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
